<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class PlayerResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return [
            'player_id' => $this->id,
            'name' => $this->name,
            'polls' => $this->polls,
            'cover' => $this->cover_url,
            'pictures' => $this->picture_urls,
            'clicks' => $this->clicks,
            'description' => $this->description,
            'rank' => [
                'rank' => $this->rank,
                'behind_last' => $this->behindLast,
                'behind_first' => $this->behindFirst,
            ],
            'counts' => [
                'likes' => $this->likes()->sum('polls'),
                'comments' => $this->comments()->sum('polls'),
                'supports' => $this->supports()->sum('polls'),
            ],
            'rank_url' => $this->getRankUrl(),
        ];
    }
}
